<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\Movies as moviesModel;
class Movies extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {

        if($request->isAjax()){

            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

            $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['a.create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['a.create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['a.title|a.actors|a.directors','like',"%{$params['seach_text']}%"];
            }

            $data = moviesModel::alias('a')->where($where)
                ->field('a.id,a.poster,a.title,a.pv,a.grade,a.is_hot,a.is_recommend,a.status,a.create_time,b.region_name,c.type_name')
                ->leftJoin('region b','a.region_id=b.id')
                ->leftJoin('movie_type c','a.movie_type=c.id')
                ->order('a.id desc')
                ->limit($offset,$pageSize)->select();

            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }
            }

            if(!empty($where)){
                $count = count($data);
            }else{
                $count = moviesModel::count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'list'=>$data,'pageData'=>$pageData];

        }

        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $types = db('movie_type')->order('rank desc')->select();
        $regions = db('region')->order('rank desc')->select();


        $this->assign('types',$types);
        $this->assign('regions',$regions);
        $this->assign('languages',config('web.language'));


        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){

            $data = $request->post();

            $model = new moviesModel($data);

            if(!empty($data['movie_time'])){
                $model->movie_time = strtotime($data['movie_time']);
            }

            //插入数据
            $model->create_time = time();
            $res = $model->allowField(true)->save();

            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'新增失败'];
            }

        }
    }



    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        
        $info = moviesModel::find($id);
        $this->assign('info',$info);

        $types = db('movie_type')->order('rank desc')->select();
        $regions = db('region')->order('rank desc')->select();


        $this->assign('types',$types);
        $this->assign('regions',$regions);
        $this->assign('languages',config('web.language'));
        return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){

            $data = $request->post();

            $model = new moviesModel();

            if(!empty($data['movie_time'])){
                $data['movie_time']= strtotime($data['movie_time']);
            }
            $res = $model->allowField(true)->save($data,['id'=>$id]);

            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'修改失败'];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('ids');
        $res = moviesModel::destroy($id);

        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }
}
